package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.KnowledgePoint;
import cn.ztydata.teachingsystem.heaven.service.KpService;
import cn.ztydata.teachingsystem.heaven.web.dto.ChapterKpDto;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultId;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.MessageFormat;
import java.util.List;

/**
 * 章节知识点控制器类
 * Created by chenxin on 2014/12/16.
 */
@Controller
@RequestMapping("/chapterKp")
public class ChapterKpController extends BaseController {
    @Autowired
    private KpService kpService;

    /**
     * 新增章节知识点
     * @param kpDto 知识点dto类
     * @return json
     *
     * @author cx
     * @since 2014-12-08
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultId saveKp(@Valid @RequestBody ChapterKpDto kpDto) {
        //映射dto类到实体类
        KnowledgePoint kp = mapper.map(kpDto, KnowledgePoint.class);

        try {
            //新增课程知识点
            kpService.saveChapterKp(kp);
        } catch (ServiceException ex) {
            log.debug("新增章节知识点失败");
            return new JsonResultId(false, ex.getMessage().isEmpty() ? "保存失败" : ex.getMessage());
        }

        log.debug("新增章节知识点成功");
        return new JsonResultId(kp.getId(), "保存成功");
    }

    /**
     * 删除章节知识点
     * @param kpId 知识点ID
     * @param nodeId 节点ID
     * @return json
     *
     * @author cx
     * @since 2014-12-09
     */
    @RequestMapping(value = "/remove/{kpId}/{nodeId}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult deleteKp(@PathVariable long kpId, @PathVariable long nodeId) {
        //校验参数
        Assert.isTrue(kpId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "知识点ID"));
        Assert.isTrue(nodeId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "节点ID"));

        try {
            //删除章节知识点
            kpService.deleteChapterKp(kpId, nodeId);
        } catch (ServiceException ex) {
            log.debug("删除章节知识点失败");
            return new JsonResult(false, ex.getMessage().isEmpty() ? "删除失败" : ex.getMessage());
        }

        log.debug("删除章节知识点成功");
        return new JsonResult(true, "删除成功");
    }

    /**
     * 获取章节的知识点集合
     * @param nodeId 节点ID
     * @return json
     *
     * @author cx
     * @since 2014-12-16
     */
    @RequestMapping(value = "/list/{nodeId}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList<ChapterKpDto> getKps(@PathVariable long nodeId) {
        //校验参数
        Assert.isTrue(nodeId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "节点ID"));

        //获取章节的知识点集合
        List<KnowledgePoint> kpList = kpService.getKpsForChapter(nodeId);

        //集合映射到dto
        List<ChapterKpDto> resultList = mapper.mapList(kpList, ChapterKpDto.class);

        log.debug("获取章节的知识点集合");
        return new JsonResultList<>(resultList);
    }
}
